const { createService, removeService, updateService, findByIdService, findByPageService } = require('../services/blogTypeService');
const AppError = require('../utils/AppError');
const Controller = require('../utils/Controller');
const { toi } = require('../utils/tools');

class BlogTypeController extends Controller {
	// 创建
	async create(req, res, next) {
		const schema = req.joi.object({
			name: req.joi.string().required(),
			articleCount: req.joi.number(),
			order: req.joi.number().required()
		});
		const { error: failed } = schema.validate(req.body);
		if (failed) return next(new AppError(failed, 400));

		await toi(createService(req.body), res, next);
	}

	// 删除
	async remove(req, res, next) {
		await toi(removeService(req.params.id), res, next);
	}

	// 更新
	async update(req, res, next) {
		const schema = req.joi.object({
			name: req.joi.string(),
			articleCount: req.joi.number(),
			order: req.joi.number()
		});
		const { error: failed } = schema.validate(req.body);
		if (failed) return next(new AppError(failed, 400));

		await toi(updateService(req.body, req.params.id), res, next);
	}

	// 查全部
	async findByPage(req, res, next) {
		await toi(findByPageService(), res, next);
	}

	// 查单个
	async findById(req, res, next) {
		await toi(findByIdService(req.params.id), res, next);
	}
}

module.exports = new BlogTypeController();

// 同时教育多个对象
// async function update(req, res, next) {
// 	const schemaParams = req.joi
// 		.object({
// 			id: req.joi.string().regex(/^\d+$/).required()
// 		})
// 		.unknown(true);
// 	const schemaBody = req.joi.object({
// 		name: req.joi.string(),
// 		articleCount: req.joi.number(),
// 		order: req.joi.number()
// 	});
// 	const schema = req.joi.object({
// 		params: schemaParams,
// 		body: schemaBody
// 	});
// 	const { error: failed } = schema.validate({
// 		params: req.params,
// 		body: req.body
// 	});
// 	if (failed) return next(new AppError(failed, 400));

// 	await toi(updateService(req.body, req.params.id, next), res, next);
// }
